package com.easyexcle.demo.easyExcel.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lkz
 * @Title: QuarterGrouping2
 * @Description: 算出时间坐在的季度
 * @Date: 2023/5/20 14:54
 */

public class QuarterGrouping2 {


    public static void main(String[] args) {
        // 定义开始时间和结束时间
        LocalDate start = LocalDate.of(2022, 10, 1);
        LocalDate end = LocalDate.of(2023, 3, 31);

        // 定义时间格式化器和季度-时间段映射
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-QQQ");
        Map<String, String> quarterMap = new HashMap<>();

        // 循环遍历时间段内的每一个月份
        LocalDate current = start;
        while (!current.isAfter(end)) {
            // 计算当前月份所在季度
            String quarter = current.format(formatter);
            String quarterRange = current.getYear() + "-" + quarter.substring(5);

            // 将当前月份添加到对应季度的时间段中
            if (quarterMap.containsKey(quarter)) {
                quarterMap.put(quarter, quarterMap.get(quarter) + ", " + quarterRange);
            } else {
                quarterMap.put(quarter, quarterRange);
            }

            // 将当前日期加上一个月
            current = current.plusMonths(1);
        }

        // 输出结果
        for (String quarter : quarterMap.keySet()) {
            System.out.println(quarter + ": " + quarterMap.get(quarter));
        }
    }

}
